package com.gg.hospitalmanage.hospitalmanage.controller;

import com.gg.common.result.CommonResult;

import com.gg.hospitalmanage.hospitalmanage.service.ApiService;
import com.gg.hospitalmanage.hospitalmanage.service.HospitalService;
import com.gg.hospitalmanage.hospitalmanage.util.HttpRequestHelper;
import com.gg.hospitalmanage.hospitalmanage.util.MD5;
import com.gg.hospitalmanage.hospitalmanage.util.RegistrationException;
import com.gg.hospitalmanage.hospitalmanage.util.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author qy
 *
 */
@RestController
public class HospitalController {

	@Autowired
	private HospitalService hospitalService;

	@Autowired
	private ApiService apiService;

	/**
	 * 预约下单
	 * @param request
	 * @return
	 */
	@PostMapping("/order/submitOrder")
	public CommonResult AgreeAccountLendProject(HttpServletRequest request, HttpServletResponse response) {
		try {
			Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

			/**
			 * 这里也有个坑。。加密的坑，我这里直接用工具类直接加密判断了
			 */
//			测试
//			System.out.println("第一行："+apiService.getSignKey());
//			System.out.println("第二行："+MD5.encrypt(apiService.getSignKey()));
//			System.out.println("第三行："+paramMap.get("sign").toString());

			if(!MD5.encrypt(apiService.getSignKey()).equals(paramMap.get("sign").toString())) {
				throw new RegistrationException(ResultCodeEnum.SIGN_ERROR);
			}

			Map<String, Object> resultMap = hospitalService.submitOrder(paramMap);
			return new CommonResult(com.gg.common.result.ResultCodeEnum.SUCCESS,resultMap);
		} catch (RegistrationException e) {
			return CommonResult.fail(e.getMessage());
		}
	}

	/**
	 * 更新支付状态
	 * @param request
	 * @return
	 */
	@PostMapping("/order/updatePayStatus")
	public CommonResult updatePayStatus(HttpServletRequest request, HttpServletResponse response) {
		try {
			Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
			if(!HttpRequestHelper.isSignEquals(paramMap, apiService.getSignKey())) {
				throw new RegistrationException(ResultCodeEnum.SIGN_ERROR);
			}

			hospitalService.updatePayStatus(paramMap);
			return new CommonResult(com.gg.common.result.ResultCodeEnum.SUCCESS);
		} catch (RegistrationException e) {
			return CommonResult.fail(e.getMessage());
		}
	}

	/**
	 * 更新取消预约状态
	 * @param request
	 * @return
	 */
	@PostMapping("/order/updateCancelStatus")
	public CommonResult updateCancelStatus(HttpServletRequest request, HttpServletResponse response) {
		try {
			Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
			if(!HttpRequestHelper.isSignEquals(paramMap, apiService.getSignKey())) {
				throw new RegistrationException(ResultCodeEnum.SIGN_ERROR);
			}

			hospitalService.updateCancelStatus(paramMap);
			return new CommonResult(com.gg.common.result.ResultCodeEnum.SUCCESS);
		} catch (RegistrationException e) {
			return CommonResult.fail(e.getMessage());
		}
	}
}

